Class 6 Exam  >  Class 6 Questions  >  What will be the output of the following C co... Start Learning for Free
What will be the output of the following C code?
#include <stdio.h>
    int main()
    {
        const int i = 10;
        int *ptr = &i;
        *ptr = 20;
        printf("%d\n", i);
        return 0;
    }
  • a)
    Compile time error
  • b)
    Compile time warning and printf displays 20
  • c)
    Undefined behaviour
  • d)
    10
Correct answer is option 'B'. Can you explain this answer?
Most Upvoted Answer
What will be the output of the following C code?#include <stdio.h&g...
Changing const variable through non-constant pointers invokes compiler warning.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
20
Free Test
Community Answer
What will be the output of the following C code?#include <stdio.h&g...

Explanation:

Constant integer 'i' and pointer 'ptr'
- The code declares a constant integer 'i' with a value of 10.
- It then declares a pointer 'ptr' that points to the address of the constant integer 'i'.

Changing the value using pointer
- Since 'i' is declared as a constant integer, it cannot be modified directly.
- However, the code tries to change the value of 'i' through the pointer 'ptr' by assigning 20 to '*ptr'.

Compile time warning and output
- The code will give a compile-time warning because it is trying to modify a constant variable.
- However, the code will still compile and run, and the printf statement will display 20 instead of 10.

Therefore, the correct output will be: Compile time warning and printf displays 20.
Explore Courses for Class 6 exam
Question Description
What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? for Class 6 2025 is part of Class 6 preparation. The Question and answers have been prepared according to the Class 6 exam syllabus. Information about What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for Class 6 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer?.
Solutions for What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for Class 6. Download more important topics, notes, lectures and mock test series for Class 6 Exam by signing up for free.
Here you can find the meaning of What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer?, a detailed solution for What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following C code?#include <stdio.h> int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%d\n", i); return 0; }a)Compile time errorb)Compile time warning and printf displays 20c)Undefined behaviourd)10Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice Class 6 tests.
Explore Courses for Class 6 exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev